package com.ykg.controller;

import com.ykg.annotation.PassToken;
import com.ykg.dto.ListDTO;
import com.ykg.dto.CUserDTO;
import com.ykg.dto.OwnerDTO;
import com.ykg.enums.ResultEnum;
import com.ykg.interceptor.ScopeLevel;
import com.ykg.request.AddListRequest;
import com.ykg.service.IListService;
import com.ykg.service.IOwnerService;
import com.ykg.util.ResultResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * TODO
 * 类(接口)：与货主相关的接口
 * Created on 9:32 2021/6/23.
 *
 * @Author: laobian
 * @Description:
 */

@Api(tags = "货主APi")
@RestController
@RequestMapping("/api/owner")

public class OwnerController {
    @Autowired
    private IOwnerService ownerService;
    @Autowired
    private IListService listService;

    @PassToken
    @PostMapping(value = "/login")
    @ApiOperation("货主登录")
    public ResultResponse login(@RequestParam String ownerName,
                                @RequestParam  String password) {
        if (ownerName == null || password == null) {

           return new ResultResponse(ResultEnum.PARAM_ERROR);
        }
        OwnerDTO ownerDTO = ownerService.login(ownerName, password);
        if (Objects.isNull(ownerDTO)) {
            return new ResultResponse(ResultEnum.LOGIN_ERROR);
        }
        return new ResultResponse(ownerDTO);

    }

    @PassToken
    @GetMapping(value = "/all",produces = "application/json")
    @ApiOperation("获取该货主所有卸货清单")
    @ScopeLevel(value = 0)
    public ResultResponse findAllList(@RequestParam long ownerId){
        return new ResultResponse(listService.findListByOwnerId(ownerId));
    }

    //@PassToken
    @PostMapping(value = "/add")
    @ApiOperation("货主提交一个卸货清单")
    @ScopeLevel(value = 0)
    public ResultResponse addList(@RequestBody  AddListRequest addListRequest){

        if(Objects.isNull(addListRequest)){
            return new ResultResponse(ResultEnum.PARAM_ERROR);
        }
        int status = listService.addList(addListRequest);
        if(status==0){
            return new ResultResponse(ResultEnum.FAILURE);
        }

        return  new ResultResponse(ResultEnum.SUCCESS);
    }

    //@PassToken
    @GetMapping(value = "/detail",produces = "application/json")
    @ApiOperation("货主查看卸货清单详情")
    @ScopeLevel(value = 0)

    public ResultResponse listDetail(@RequestParam String listId){
        if(Objects.isNull(listId)){
            return  new ResultResponse(ResultEnum.PARAM_ERROR);
        }
        ListDTO listDTO = listService.findListByListId(listId);
        if(Objects.isNull(listDTO)){
            return new ResultResponse(ResultEnum.FAILURE);
        }
        return new ResultResponse(listDTO);
    }


}
